using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Timers;

public class KillerBehavior : MonoBehaviour
{
    // Start is called before the first frame update
    public float default_speed = 100000.0f;
    private Rigidbody rigid_body;
    private Transform m_transform;//摄像机父物体Transform
    private System.Random rd = new System.Random((int)System.DateTime.Now.Ticks);
    private Timer t;

    private bool isDestroyed=false;
    /// <summary>
    /// OnCollisionEnter is called when this collider/rigidbody has begun
    /// touching another rigidbody/collider.
    /// </summary>
    /// <param name="other">The Collision data associated with this collision.</param>
    void OnCollisionEnter(Collision other)
    {
        if(other.gameObject.tag == "wall")
            isDestroyed=true;
        else if(other.gameObject.tag == "killer")
        {
            int interval = 1000;
            t = new Timer();
            t.Stop();
            t.Elapsed += TimerDestroyCallBackHook;
            t.Interval = interval;
            t.AutoReset = true;
            t.Start();
        }
    }

    void TimerDestroyCallBackHook(object source, System.Timers.ElapsedEventArgs e)
    {
        t.Stop();
        isDestroyed=true;
    }

    void OnBecameInvisible()
    {
        Destroy(this.gameObject);  //删除游戏对象
    }

    void Start()
    {
        int rand_pos;
        m_transform = GetComponent<Transform>();
        rigid_body = GetComponent<Rigidbody>();
        rigid_body.velocity -= Vector3.forward * default_speed; 
        rand_pos =  rd.Next()%30 - 15;
        m_transform.position = Vector3.right * rand_pos;
        m_transform.position += Vector3.forward * 25;   
        m_transform.position += Vector3.up * 3;   
    }

    // Update is called once per frame
    void Update()
    {
        if(isDestroyed)
            Destroy(this.gameObject);
    }
}
